import {checkUserName} from "@/api/system/user.js";
import {reactive} from "vue";

export function useValidateUser({ userId,query =  {} }) {
    const formState = reactive(Object.assign({
        userIdentity:1,
        activitiSync:1,
    },query))
    const validateUsername = async (rule, value) => {
        let params = {
            tableName: 'app_user',
            fieldName: 'username',
            fieldVal: value,
            dataId: userId.value
        };
        const res = await checkUserName(params)
        if (res.success) {
            return Promise.resolve()
        } else {
            return Promise.reject("用户名已存在!")
        }
    }

    const reset = () => {
        formState.userIdentity = 1
        formState.activitiSync = 1
    }

    const compareToFirstPassword  = (rule, value) => {
        console.log(value)
        console.log(formState.password)
        if (value && value !== formState.password) {
            return Promise.reject('两次输入的密码不一样！');
        } else {
            return Promise.resolve();
        }
    }
    const validateEmail = async (rule, value) => {
        if(!value){
            return Promise.resolve()
        }else{
            if(new RegExp(/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/).test(value)){
                // let params = {
                //     tableName: 'app_user',
                //     fieldName: 'email',
                //     fieldVal: value,
                //     dataId: userId
                // };
                // const res = await checkUserName(params)
                // if (res.success) {
                //     return Promise.resolve()
                // } else {
                //     return Promise.reject("邮箱已存在!")
                // }
            }else{
                return Promise.reject("请输入正确格式的邮箱!")
            }
        }
    }
    return{
        validateUsername,
        compareToFirstPassword,
        formState,
        validateEmail,
        reset
    }
}

